package org.example.day;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: Solution624
 * @Author wyatt
 * @Data 2025/02/19 9:32
 */
public class Solution624 {

    public static void main(String[] args) {

        List<List<Integer>> arrays = new ArrayList<>();
        arrays.add(Arrays.asList(1,2,3));
        arrays.add(Arrays.asList(4,5));
        arrays.add(Arrays.asList(1,2,3));
        System.out.println(new Solution624().maxDistance(arrays));
    }

    public int maxDistance(List<List<Integer>> arrays) {

        int[] minArr = new int[2];
        int[] maxArr = new int[2];
        Arrays.fill(minArr, Integer.MAX_VALUE / 2);
        Arrays.fill(maxArr, Integer.MIN_VALUE / 2);
        int minIndex = -1;
        int maxIndex = -1;

        for (int i = 0; i < arrays.size(); i++) {
            int min = arrays.get(i).get(0);
            int max = arrays.get(i).get(arrays.get(i).size() - 1);

            if(min < minArr[0]){
                minIndex = i;
                minArr[1] = minArr[0];
                minArr[0] = min;
            } else if (min < minArr[1]) {
                minArr[1] = min;
            }

            if(max > maxArr[0]){
                maxIndex = i;
                maxArr[1] = maxArr[0];
                maxArr[0] = max;
            } else if (max > maxArr[1]) {
                maxArr[1] = max;
            }

        }

        if(minIndex != maxIndex){
            return Math.abs(maxArr[0] - minArr[0]);
        }

        return Math.max(Math.abs(maxArr[0] - minArr[1]), Math.abs(maxArr[1] - minArr[0]));
    }

}
